//index.js
//获取应用实例
const app = getApp()
//拿到全局变量的值
var title = app.globalData.title
var content = app.globalData.content


Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    title: null,
    content: null

  },
  //事件处理函数
  goToDetails: function () {
    //console.log('its success')
    wx.navigateTo({
      url: '../details/details',
      success: function (res) {
        console.log(res)
      },
      fail: function (res) {
        console.log(res)
      }
    })
  },

  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  onShow: function () {
    var that = this;
    //console.log(title.length)
    //console.log(content)

    //把获得的全局变量的值发送到页面的 data里
    that.setData({
      title: title,
      //content: content
    })

    //获取页面缓存数据
    var that = this;
    wx.getStorage({
      key: 'inputMsg',
      success: function (e) {
        that.setData({
          msgTitle: e.data.title,
          'globalData.title': e.data.title
        })

        // console.log(e.data.title)
      }
    });
  },
  getUserInfo: function (e) {
    //console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  //点击删除标题
  deleteTitle: function(e){
    var that = this;
    for(var i=0; i<title.length; i++){
      //e.currentTarget.dataset.index 这个是获取在 html里绑定的自定义属性 index
      //通过这种方法把当前点击的元素的下标拿到
      //console.log(e.currentTarget.dataset.index)
      //console.log(title[i])
      if(i === e.currentTarget.dataset.index){
        //console.log(i)
        //点击时删除当前项
        title.splice(i, 1);
        //把删除后的数组赋值给出去
        that.setData({
          title: title
        })

      }
    }
  }
})